package com.bw.finance.controller;



import com.bw.finance.conf.ResponseResult;
import com.bw.finance.eneity.TbMenu;
import com.bw.finance.eneity.res.TbMenuRes;
import com.bw.finance.service.ITbMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhengqian
 * @since 2020-05-09
 */
@RestController
@RequestMapping("/menu")
public class TbMenuController {
    @Autowired
    private ITbMenuService tbMenuService;

   @GetMapping("/getTree")
   public ResponseResult list(){
       ResponseResult result = ResponseResult.SUCCESS();
       try {
          List<TbMenuRes> list=tbMenuService.findlist();
          result.setResult(list);
       }catch (Exception e){
           result.setSuccess(false);
           result.setMessage("查询失败"+e.toString());
       }
       return result;
   }
    @PostMapping("/savemenu")
    public ResponseResult savemenu(@RequestBody TbMenu tbMenu){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            tbMenuService.saveOrUpdate(tbMenu);
            result.setMessage("保存成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("保存失败"+e.toString());
        }
        return result;
    }

    @GetMapping("/del/{id}")
    public ResponseResult list(@PathVariable Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            tbMenuService.remove(id);
            result.setMessage("删除成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("删除失败"+e.toString());
        }
        return result;
    }
    @GetMapping("/getone/{id}")
    public ResponseResult getone(@PathVariable Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            TbMenu tbMenu = tbMenuService.getById(id);
            result.setResult(tbMenu);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return result;
    }
    @GetMapping("/getTreeById/{id}")
    public ResponseResult getTreeById(@PathVariable Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            List<TbMenuRes> list=tbMenuService.getTreeById(id);
            result.setResult(list);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return result;
    }
}
